#include <stdio.h>
#include <stdlib.h>
#include "double_list.h"
#include <string.h>

DListRet print(void *arg, void *data)
{
    printf("data: %s\n", (char *)(data));
}

int d_cmp(void *data1, void *data2)
{
    char *a = (char *)data1;
    char *b = (char *)data2;

    if (strcmp(a, b) == 0)
    {
	return 0;
    }

    return -1;
}

DListRet get_max(void *arg, void *data)
{
    int *a = (int *)arg;
    int b = (int)data;

    if (*a < b)
    {
	*a = b;
    }
    return DListRetOk;
}

DListRet get_sum(void *sum, void *data)
{
    int *a = (int *)sum;
    int b = (int)data;

    *a += b;

    return DListRetOk;
}

DListRet convert(void *buf, void *data)
{
    char *a = (char *)buf;
    char *b = (char *)data;

    while (*b != '\0')
    {
	if (isupper(*b))
	{
	    *b = tolower(*b);
	}
	b++;
    }

    return DListRetOk;
}

int main(int argc, char *argv[])
{
    int i = 0;
    int j = 0;
    int max = 0;
    int sum = 0;
    char str[64] = {0};
    char *buf = NULL;
    _DNode *node = NULL;
    _DList head = create_list();

    for(i; i < 10; i++)
    {
	printf("please input string\n");
	gets(str);
	buf = (char *)malloc(strlen(str));
	memcpy(buf, str, strlen(str));
	node = create_node((void *)buf);
	insert_to_list(head, node);
    }

    dlist_foreach(head, print, NULL);

    printf("please input the number you want to delete:\n");
    scanf("%s", str);
    delete_node(head, d_cmp, (void *)str);
    dlist_foreach(head, print, NULL);

    dlist_foreach(head, convert, NULL);
    dlist_foreach(head, print, NULL);
    return 0;
}

